package dao

import (
	"album/model"
	"context"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
)

type BaseCategory3Dao struct {
	conn *gorms.GormConn
}

func (u *BaseCategory3Dao) FindByCategory3To8(ctx context.Context, category2Id int64) (category3s []*model.BaseCategory3, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory3{}).Where("category2_id = ? and is_top = 1", category2Id).Limit(7).Find(&category3s).Error
	if err != nil {
		return nil, err
	}
	return category3s, nil
}
func (u *BaseCategory3Dao) FindByCategory3To8Desc(ctx context.Context, category2Id int64) (category3s []*model.BaseCategory3, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory3{}).Where("category2_id = ? and is_top = 1", category2Id).Order("order_num desc").Limit(8).Find(&category3s).Error
	if err != nil {
		return nil, err
	}
	return category3s, nil
}

// 查找二级分类id
func (u *BaseCategory3Dao) FindByCategory3ToC2Id(ctx context.Context, id int64) (c2id int64, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory3{}).Select("category2_id").Where("id = ?", id).Scan(&c2id).Error
	if err != nil {
		return 0, err
	}
	return c2id, nil
}

// 查找一级分类id
func (u *BaseCategory3Dao) FindByCategory2ToC1Id(ctx context.Context, id int64) (c1id int64, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory2{}).Select("category1_id").Where("id = ?", id).Scan(&c1id).Error
	if err != nil {
		return 0, err
	}
	return c1id, nil
}
func NewBaseCategory3Dao(db *db.ListenDB) *BaseCategory3Dao {
	return &BaseCategory3Dao{
		conn: gorms.New(db.Conn),
	}
}
func (u *BaseCategory3Dao) FindByCategory3(ctx context.Context, category2Id int64) (category3s []*model.BaseCategory3, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory3{}).Where("category2_id = ?", category2Id).Find(&category3s).Error
	if err != nil {
		return nil, err
	}
	return category3s, nil
}
func (u *BaseCategory3Dao) FindCategory3ById(ctx context.Context, id int64) (category3 *model.BaseCategory3, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory3{}).Where("id = ?", id).First(&category3).Error
	if err != nil {
		return nil, err
	}
	return category3, nil
}
